#include<vector>
using namespace std;


struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};
 
class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        auto head = root;
        int num = 0;
        while (head != NULL) {
            head = head->next;;
            ++num;
        }
        int min = num / k;
        int prevNum = num % k;
        vector<ListNode*> res;
        for (int i = 0; i < k; ++i) {
            head = root;
            int n = min + (i < prevNum ? 1 : 0);
            ListNode* prev=NULL;
            while (n > 0) {
                prev = root;
                root = root->next;
                --n;
            }
            if (prev != NULL) 
                prev->next = NULL;
            res.push_back(head);
        }
        return res;
    }
};